﻿using BJ.AuthDomain.IService;
using BJ.AuthEntity.Models;
using BJ.AuthEntity.ViewModels;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Syspetro.Core.AppDeal;
using Syspetro.Core.DynamicApi;
using Syspetro.Deploy.AuthorizationReq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BJ.AuthApp.Admin
{
    /// <summary>
    /// 
    /// </summary>
    [Authorize]
    [Admin]
    public class MenuApp : BaseDynamicApi
    {
        readonly IMenuService service;
        public MenuApp(IMenuService _Service)
        {
            service = _Service;
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="vrole"></param>
        /// <returns></returns>
        public async Task<SpActionResult<Guid>> AddMenu(VM_Menu_Add vrole)
        {
            if (string.IsNullOrEmpty(vrole.Name))
                return Fail<Guid>("名称不能为空");
            return Result(await service.AddAsync(vrole.Adapt<Menu>()));
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="vrole"></param>
        /// <returns></returns>
        public async Task<SpActionResult<int>> AddMenus(List<VM_Menu_Add> vrole)
        {
            return Result(await service.AddAsync(vrole.Adapt<List<Menu>>()));
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SpActionResult<bool>> DeleteMenu(Guid id)
        {
            return Result(await service.DeleteAsync(id));
        }
        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="role_Update"></param>
        /// <returns></returns>
        public async Task<SpActionResult<bool>> UpdateMenu(VM_Menu_Update role_Update)
        {
            return Result(await service.UpdateAsync(role_Update, t => t.Id == role_Update.Id));
        }
        /// <summary>
        /// 查看菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SpActionResult<VM_Menu>> GetMenu(Guid id)
        {
            var list = await service.GetAsync(id);
            return Result(list.Adapt<VM_Menu>());
        }
        /// <summary>
        /// 分页查找所有菜单
        /// </summary>
        /// <param name="pageReq"></param>
        /// <returns></returns>
        [ProducesResponseType(typeof(VM_Menu), 200)]
        public async Task<SpActionResult<PageList<VM_Menu>>> FindMenus([FromBody] VM_Menu_PageReq pageReq)
        {
            if (pageReq == null)
                pageReq = new VM_Menu_PageReq();
            return Result(await service.FindPageAsync<VM_Menu>(pageReq));
        }
        /// <summary>
        /// 查找所有菜单
        /// </summary>
        /// <returns></returns>
        [ProducesResponseType(typeof(VM_Menu), 200)]
        public async Task<SpActionResult<List<VM_Menu>>> GetMenus()
        {
            
            return Result(await service.FindDtoAsync<VM_Menu>(d=>true));
        }
        /// <summary>
        /// 菜单授权
        /// </summary>
        /// <param name="authMenus"></param>
        /// <returns></returns>
        public async Task<SpActionResult<bool>> AuthMenus(VM_AuthMenus authMenus)
        {
            return new SpActionResult<bool>(await service.AuthMenus(authMenus));
        }
        /// <summary>
        /// 树形菜单列表查询
        /// </summary>
        /// <returns></returns>
        public async Task<SpActionResult<List<VM_Menu>>> FindMenusAsTreeCte()
        {
            return new SpActionResult<List<VM_Menu>>(await service.FindMenusAsTreeCte());
        }
    }
}
